package com.gitee.wsl.security.rbac

import com.gitee.wsl.struct.category.CategoryItemWarp


open class RoleSubjectPermission(
    val role: Role,
    val subjectPermission: SubjectPermission
): PermissionCheck {

    val currentSubject = subjectPermission.subject

    var isAbleAll:Boolean = false

    var isDisableAll:Boolean = false

    var baseRule:String ?= null

    override fun hasPermission( vararg permission:String):Boolean{
        if(isAbleAll) return true
        if(isDisableAll) return false

        return false
    }

    companion object{
        //val EMPTY=RoleSubjectPermission(Role(".Companion."),SubjectPermission())
    }
}

open class RoleSubjectPermissionCategory(
    val roleNode: RoleSubjectPermission
): CategoryItemWarp(roleNode), PermissionCheck {

    override fun hasPermission( vararg permission:String):Boolean{
        return roleNode.hasPermission(*permission)
    }

    fun isSubject(subjectName:String):Boolean{
        return subjectName==roleNode.currentSubject.name
    }

    fun getSubjectPermissionCategory(subject:String): RoleSubjectPermissionCategory?{
        return subItemList.find {
            var isFind=false
            if(it is RoleSubjectPermissionCategory){
                isFind= it.isSubject(subject)
            }
            isFind
        } as RoleSubjectPermissionCategory?
    }

    companion object{
        fun hasSubjectPermission(root: RoleSubjectPermissionCategory, subjectTreeNode:List<String>, vararg permission: String):Boolean{
            if(root.hasPermission(*permission)) return true

            if(subjectTreeNode.isNotEmpty() ) {
                root.getSubjectPermissionCategory(subjectTreeNode[0])?.let {
                    val subList=if(subjectTreeNode.size>1) subjectTreeNode.subList(1,subjectTreeNode.size) else subjectTreeNode
                    return hasSubjectPermission(it,subList,*permission)
                }
            }
            return false
        }
    }
}

class RoleSubjectPermissionCategoryComp(root: RoleSubjectPermission, vararg val roleSubjectPermissionCategory: RoleSubjectPermissionCategory): RoleSubjectPermissionCategory(root){

    override fun hasPermission(vararg permission: String): Boolean {
        roleSubjectPermissionCategory.forEach {
            if(it.hasPermission(*permission))
                return true
        }
        return false
    }

    companion object{

        fun buildRoot(vararg roleSubjectPermissionCategoryRoot: RoleSubjectPermissionCategory): RoleSubjectPermissionCategoryComp {
            val baseNode=roleSubjectPermissionCategoryRoot[0].data as RoleSubjectPermission
            val parent = RoleSubjectPermissionCategoryComp(baseNode, *roleSubjectPermissionCategoryRoot)

            buildNode(parent,*roleSubjectPermissionCategoryRoot)
            return parent
        }

        fun buildNode(parent: RoleSubjectPermissionCategoryComp, vararg roleSubjectPermissionCategory: RoleSubjectPermissionCategory){
            val subjectList = mutableListOf<Subject>()
            roleSubjectPermissionCategory.forEach {
                //it.roleNode.currentSubject
            }
        }
    }
}

